package android.slc.appbase.ui.dialog;

import android.slc.box.core.utils.StageUtils;
import android.slc.box.core.virtual_android.Bundle;
import cn.hutool.core.util.StrUtil;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;

import java.util.LinkedHashMap;
import java.util.Map;

public class SlcPopup {
    public static final String KEY_MSG = "message";

    private static final Map<String, Window> operateMap = new LinkedHashMap<>();

    public static void addOperate(String key, Window baseOperate) {
        if (!StrUtil.isEmpty(key) && baseOperate != null) {
            operateMap.put(key, baseOperate);
        }
    }

    public static void removeOperate(String key) {
        if (!StrUtil.isEmpty(key)) {
            operateMap.remove(key);
        }
    }

    public static void dismissByKey(String key) {
        Window baseOperate = getOperateByKey(key);
        if (baseOperate != null) {
            //removeOperate(key);
            if (baseOperate instanceof Stage) {
                Stage stage = (Stage) baseOperate;
                StageUtils.destroy(stage);
            }
        }
    }

    public static Window getOperateByKey(String key) {
        return operateMap.get(key);
    }

    public static void applyStylesheets(Scene scene) {
        scene.getStylesheets().add("/style/app.css");
        scene.getStylesheets().add("/style/project.css");
    }

    public static void initModality(Stage targetStage, Window ownerWindow) {
        if (ownerWindow != null) {
            targetStage.initModality(Modality.WINDOW_MODAL);
            targetStage.initOwner(ownerWindow);
        } else {
            targetStage.setAlwaysOnTop(true);
        }
    }

    public static void initStyle(Stage targetStage, boolean cancelable) {
        if (cancelable) {
            targetStage.initStyle(StageStyle.UTILITY);
        } else {
            targetStage.initStyle(StageStyle.UNDECORATED);
        }
    }

    public static Bundle createBundle(String message) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_MSG, message);
        return bundle;
    }

    public static String getMsg(Bundle bundle) {
        if (bundle == null) {
            return null;
        }
        return (String) bundle.getSerializable(KEY_MSG);
    }
}
